//
//  JHLoginViewController.m
//  JHChat
//
//  Created by JiaHao on 2/22/15.
//  Copyright (c) 2015 JiaHao. All rights reserved.
//

#import "JHLoginViewController.h"

@interface JHLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@property (strong,nonatomic) XMPPJID *myJid;
@property (strong, nonatomic) XMPPStream * xmppStream;

@end

@implementation JHLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.xmppStream = [JHXMPPManager sharedManager].xmppStream;
    NSUserDefaults *userDefult = [NSUserDefaults standardUserDefaults];
    NSString *userName = [userDefult objectForKey:@"username"];
    NSString *password = [userDefult objectForKey:@"password"];
    if (![userName isEqualToString:@""] && userName) {
        self.userNameTextField.text = userName;
        self.passwordTextField.text = password;
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.xmppStream removeDelegate:self];
}


- (IBAction)doLogin:(id)sender {
    [self xmppConnect];
}

-(void)xmppConnect
{
    [self.xmppStream disconnect];
    if (![self.userNameTextField.text isEqualToString:@""] && self.userNameTextField.text != nil)
    {
        //1.创建JID
        self.myJid = [XMPPJID jidWithUser:self.userNameTextField.text domain:XMPP_HOST_NAME resource:XMPP_RESOURCE];

        //2.把JID添加到xmppSteam中
        // [self.xmppStream setHostPort:5222];
        [self.xmppStream setMyJID:self.myJid];
        //连接服务器
        NSError *error = nil;
        [self.xmppStream connectWithTimeout:10 error:&error];
        if (error)
        {
            NSLog(@"连接出错：%@",[error localizedDescription]);
        }
    }else
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名不能为空" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
        [alter show];
    }
}

//连接后的回调
-(void)xmppStreamDidConnect:(XMPPStream *)sender
{
    if (![self.passwordTextField.text isEqualToString:@""] && self.passwordTextField.text != nil)
    {
        //连接成功后认证用户名和密码
        NSError *error = nil;
        [self.xmppStream authenticateWithPassword:self.passwordTextField.text error:&error];
        if (error)
        {
            NSLog(@"认证错误：%@",[error localizedDescription]);
        }
    }
    else
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码不能为空" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
        [alter show];
    }
}


//认证成功后的回调
-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
    NSLog(@"登陆成功");
    
    NSUserDefaults *userDefult = [NSUserDefaults standardUserDefaults];
    [userDefult setObject:self.userNameTextField.text forKey:@"username"];
    [userDefult setObject:self.passwordTextField.text forKey:@"password"];
    [[JHXMPPManager sharedManager].xmppStream setMyJID:self.myJid];
    [JHXMPPManager sharedManager].myStreamBareJidStr = [NSString stringWithFormat:@"%@@%@",self.userNameTextField.text,XMPP_HOST_NAME];

    //设置在线状态
    XMPPPresence * pre = [XMPPPresence presence];
    [self.xmppStream sendElement:pre];
    
    UIStoryboard *storybard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UITabBarController *viewController = [storybard instantiateViewControllerWithIdentifier:@"HomeTabbar"];
    [self presentViewController:viewController animated:YES completion:^{
    }];
}


//认证失败的回调
-(void)xmppStream:sender didNotAuthenticate:(DDXMLElement *)error
{
    NSLog(@"认证失败 : %@",error.description);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
